package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.MyWalletBean;
import com.bocai.kelalacar.bean.MyWalletBillBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.ada.MyWalletAda;
import com.bocai.kelalacar.ui.car.set.contract.MyWalletContract;
import com.bocai.kelalacar.ui.car.set.module.MyWalletModule;
import com.bocai.kelalacar.ui.car.set.presenter.MyWalletPresenter;
import com.bocai.kelalacar.util.ScreenUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by yuanfei on 2017/6/22.
 */

public class MyWalletActivity extends BaseActivity implements View.OnClickListener, MyWalletContract.view, XRecyclerView.LoadingListener {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    View headView;
    MyWalletAda ada;
    LinearLayout llHead;
    @Inject
    MyWalletPresenter presenter;
    int pageNo = 1;
    TextView tvWithdrawMoney;
    Button btnWithdraw;
    TextView tvCountMoney;
    TextView tvFreezeMoney;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    MyWalletBean bean;


    @Override
    public int getLayoutId() {
        return R.layout.act_my_wallet;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MyWalletModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("我的钱包");
        initBack(imgBack,llBack);
        rightBtn.setVisibility(View.VISIBLE);
        ada = new MyWalletAda();
        setHeadView();
        xrecycle.setLayoutManager(new LinearLayoutManager(this));
        xrecycle.addHeaderView(headView);
        xrecycle.setAdapter(ada);
        xrecycle.setLoadingListener(this);
        rightBtn.setOnClickListener(this);
        xrecycle.refresh();

    }

    public void setHeadView() {
        headView = getLayoutInflater().inflate(R.layout.head_my_wallet, null);
        llHead = (LinearLayout) headView.findViewById(R.id.ll_head);
        btnWithdraw = (Button) headView.findViewById(R.id.btn_withdraw);
        tvWithdrawMoney = (TextView) headView.findViewById(R.id.tv_withdraw_money);
        tvCountMoney = (TextView) headView.findViewById(R.id.tv_count_money);
        tvFreezeMoney = (TextView) headView.findViewById(R.id.tv_freeze_money);
        btnWithdraw.setOnClickListener(this);
        setWidth();
    }

    public void setWidth() {
        LinearLayout.LayoutParams para1;
        para1 = (LinearLayout.LayoutParams) llHead.getLayoutParams();
        para1.width = ScreenUtils.getScreenWidth(this);
        llHead.setLayoutParams(para1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_withdraw:
                startActivityForResult(new Intent(MyWalletActivity.this,WithdrawActivity.class).putExtra("money",bean.getAvailable()),222);
                break;
            case R.id.right_btn:
                startActivity(WithdrawTypeActivity.class);
                break;
        }
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        presenter.myWallet();
        presenter.myWalletBill(pageNo + "", "10");
    }

    @Override
    public void onLoadMore() {
        pageNo = pageNo + 1;
        presenter.myWalletBill(pageNo + "", "10");
    }

    @Override
    public void myWalletSuccess(MyWalletBean bean) {
        this.bean = bean;
        tvCountMoney.setText((bean.getAvailable()+bean.getWithdrawals())+"");
        tvFreezeMoney.setText(bean.getWithdrawals()+"");
        tvWithdrawMoney.setText(bean.getAvailable()+"");
    }

    @Override
    public void myWalletBillSuccess(MyWalletBillBean billBean) {
        xrecycle.refreshComplete();
        xrecycle.loadMoreComplete();
        if (billBean!=null&&billBean.getBills()!=null){
            if (pageNo == 1){
                ada.clear();
            }
            ada.addItems(billBean.getBills());
            if (billBean.getBills().size()<10){
                xrecycle.setLoadingMoreEnabled(false);
            }else {
                xrecycle.setLoadingMoreEnabled(true);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 222&&resultCode == 222){
            xrecycle.refresh();
        }
    }
}
